package org.ftc.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.ftc.domain.CTMap;
import org.ftc.form.CommitteeTeamForm;
import org.ftc.service.CTMapService;
import org.ftc.service.FtcLogService;
import org.ftc.service.FtcLogService.Log;
import org.ftc.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller 
public class CTMapController {
	@Autowired
	UsersService usersService;
	@Autowired
	CTMapService ctMapService;
	@Autowired
	FtcLogService logService;
	
	@RequestMapping(value = "/ctmap.htm")
	public String codeCause(ModelMap model) {
		return "ctmap/list";
	}

	@RequestMapping(value = "/ctmap/list.json", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> find(CommitteeTeamForm form, ModelMap model) {
		logService.before();
		Map<String, Object> json = new HashMap<String, Object>();
		List<CTMap> data = ctMapService.findAll(form.gettCode());
		json.put("rows", data);
		logService.save(Log.QUERY, this.getClass().toString());
		return json;
	}

	@RequestMapping(value = "/ctmap/update.json", method = RequestMethod.POST)
	public @ResponseBody boolean save(CTMap entity,BindingResult binding, ModelMap model) {
		logService.before();
		boolean result = false;
		if(entity==null || StringUtils.isBlank(entity.getCtSeq())){
			return true;
		}
		CTMap ctmap = ctMapService.get(entity.getCtSeq());
		ctmap.setSort(entity.getSort());
		result = ctMapService.update(ctmap);
		logService.save(Log.UPDATE, entity.getCtSeq(), this.getClass().toString() ,ToStringBuilder.reflectionToString(entity));
		return result;
	}
	
	@RequestMapping(value = "/ctmap/delete/{seq}.json")
	public @ResponseBody boolean delete(@PathVariable String seq) {
		logService.before();
		Boolean s= false;
		if(StringUtils.isBlank(seq)){
			return s;
		}
		s = ctMapService.delete(seq);
		logService.save(Log.DELETE, seq, this.getClass().toString());
		return s;  
	}
	
	@RequestMapping(value = "/ctmap/findAll.json", method = RequestMethod.GET)
	public @ResponseBody
	List<CTMap> findAll() {
		return ctMapService.findAll();
	}

}
